package com.wxjfkg.workflow.impl;

import java.math.BigDecimal;

import org.activiti.engine.delegate.DelegateExecution;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fivestar.base.vo.Constants;
import com.fivestar.credit.service.CreditApplyInfoService;
import com.fivestar.credit.service.CreditLimitService;
import com.fivestar.credit.service.vo.CreditChangeApplyDto;
import com.fivestar.credit.service.vo.CreditConstants;
import com.fivestar.credit.service.vo.CreditLimitDto;
import com.fivestar.query.vo.ResultDto;
import com.wxjfkg.workflow.BeforeEndTaskService;

@Service("creditChangeSuccessService")
public class CreditChangeSuccessService implements BeforeEndTaskService {

	private Logger logger = LoggerFactory.getLogger(CreditChangeFailService.class);
	
	@Autowired
	private CreditLimitService creditLimitService;
	@Autowired
	private CreditApplyInfoService creditApplyInfoService;

	@Override
	@Transactional(value="txTranManager")
	public void execute(DelegateExecution execution) throws Exception {
		String bussnessKey  = execution.getProcessBusinessKey();
		logger.info("CreditChangeFailService,{}",bussnessKey);
		
		CreditChangeApplyDto sucApply = new CreditChangeApplyDto();
		sucApply.setChangeApplyId(bussnessKey);
		sucApply.setAppStat(Constants.CreditChangeStatus.one);//申请成功
		creditApplyInfoService.updateCreditChangeApplyStatus(sucApply);
		
		ResultDto<CreditChangeApplyDto> retDto = creditApplyInfoService.selectCreditChangeApplyById(sucApply);
		if(retDto.isSuccess()&&retDto.getData()!=null){
			sucApply = retDto.getData();
			
			ResultDto<CreditLimitDto> retCredit = creditLimitService.queryCreditById(sucApply.getCreditId());
			CreditLimitDto creditLimit = retCredit.getData();
			
			String bussType = "";
			String tranDirect = "";
			BigDecimal changeAmt = BigDecimal.ZERO;
			
			//设置业务类型,变更额度 和处理逻辑
			if(Constants.CreditChangeType.zero.equals(sucApply.getChangeType())){
				//维持
				bussType = CreditConstants.CREDIT_BUSS_TYPE.CONTINUE;
				changeAmt = BigDecimal.ZERO;
				tranDirect = CreditConstants.CREDIT_DIRECT.ADD;
			}else if(Constants.CreditChangeType.one.equals(sucApply.getChangeType())){
				//减信(调低额度)
				bussType = CreditConstants.CREDIT_BUSS_TYPE.REDUCE;
				changeAmt = sucApply.getChangeQuota();
				tranDirect = CreditConstants.CREDIT_DIRECT.MINUS;
			}else if(Constants.CreditChangeType.two.equals(sucApply.getChangeType())){
				//中止
				bussType = CreditConstants.CREDIT_BUSS_TYPE.END;
				changeAmt = creditLimit.getCreditQuota();
				tranDirect = CreditConstants.CREDIT_DIRECT.END;
			}else{
				logger.error("变更业务类型不能为空!");
				throw new RuntimeException("变更业务类型不能为空!");
			}
			ResultDto<Boolean> b = creditLimitService.mergeCreditLimit(sucApply.getMerchId(), 
												creditLimit.getCreditType(), 
												changeAmt, 
												bussType, 
												sucApply.getChangeApplyId(), 
												tranDirect);
			if(!b.isSuccess()||(b.isSuccess()&&!b.getData())){
				throw new RuntimeException("creditLimitService.mergeCreditLimit调用失败");
			}
		}
	}

}
